package com.base.cn.platform.os.controller.op.advert;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.advert.OpAdvertPlace;
import com.base.cn.platform.os.service.op.advert.OpAdvertPlaceBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 广告位置详情接口
 * @author ll
 * @date 2018/5/17 10:37
 */
@RestController
@RequestMapping("/manage/op")
public class OpAdvertPlaceController extends BaseController {

    @Autowired
    private OpAdvertPlaceBiz opAdvertPlaceBiz;

    /**
     * 添加修改广告位置消息
     * @param advertPlace
     * @return
     */
    @RequestMapping("/saveUpdateOpAdvertPlace")
    public Map<String,Object> saveUpdateOpAdvertPlace(@RequestBody OpAdvertPlace advertPlace){
        if(!StringUtils.isNotEmpty(advertPlace.getPlaceName())){
            return ResultUtil.ERROR("请填写广告位置名");
        }
        if(!StringUtils.isNotEmpty(advertPlace.getPlaceKey())){
            return ResultUtil.ERROR("请填写位置标识");
        }
        Pattern pat = Pattern.compile("^[a-zA-Z]+$");
        Matcher mat = pat.matcher(advertPlace.getPlaceKey().trim());
        if(!mat.matches()){
            return ResultUtil.ERROR("写位置标识只允许英文");
        }
        BigDecimal count = null;
        if(DataUtil.idIsNotNull(advertPlace.getId())){//修改
            count = opAdvertPlaceBiz.count(" id !="+advertPlace.getId()+" and placeKey ='"+advertPlace.getPlaceKey().trim()+"'");
        }else{//添加
            count = opAdvertPlaceBiz.count("  placeKey ='"+advertPlace.getPlaceKey().trim()+"'");
        }
        if(DataUtil.idIsNotNull(count)){
            return ResultUtil.ERROR("位置标识【"+advertPlace.getPlaceKey()+"】已存在");
        }
        advertPlace.setPlaceKey(advertPlace.getPlaceKey().trim());
        OpAdvertPlace opAdvertPlace = opAdvertPlaceBiz.saveUpdateAdvertPlace(advertPlace);
        return ResultUtil.SUCCESS("编辑成功",opAdvertPlace);
    }



    /**
     * 获取广告位置列表页面
     * @return
     */
    @RequestMapping("/queryAdvertPlaceList")
    public  List<OpAdvertPlace> queryAdvertPlaceList(){
        return opAdvertPlaceBiz.find(" 1=1 ",null,null);
    }



    /**
     * 初始化编辑广告位置页面
     * @param id 数据ID
     * @return
     */
    @RequestMapping("/queryAdvertPlaceById")
    public OpAdvertPlace queryAdvertPlaceById(@RequestParam("id") BigDecimal id){
        return opAdvertPlaceBiz.findById(id);
    }
}
